<template>
  <div class="cart">
    <!-- 使用头部组件 -->
    <My-Header header_title="购物车"></My-Header>

    <div class="empty-cart" v-if="currentCart.length !== 0 ? false : true">
      <img src="" alt="" class="empty-cart-img">
      <div class="empty-cart-text">购物车还是空的</div>
      <div class="btn" @click="jumpTo">去逛逛</div>
    </div>
    <div class="cart-container"  v-for="(item,index) in currentCart" :key="item.id" v-else>
      <!--
          选中：icon-checkbox-marked-circ
          未选中：icon-checkbox-blank-circle-outline
      -->
      <span class="iconfont toogle"
            :class = "item.selectState ? 'icon-checkbox-marked-circ':'icon-checkbox-blank-circle-outline'"
            @click="toggleSingle(index)"
      ></span>
      <div class="item-detail">
        <img :src="item.img" alt="">
        <div class="item-info">
          <h3>{{item.title}}</h3>
          <div class="pay">
            <div class="pay-price">￥{{item.price}}</div>
            <div class="edit-quantity">
              <p class="operate-btn iconfont icon-minus" @click="handleMinus(index)"></p>
              <p class="btn-input">{{item.num}}</p>
              <p class="operate-btn iconfont icon-plus" @click="handlePlus(index)"></p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="cart-footer">
      <div class="cart-footer-left">
        <span class="iconfont toogle"
            :class = "isSelectAll ? 'icon-checkbox-marked-circ':'icon-checkbox-blank-circle-outline'"
            @click="toggleAll"
        ></span>
        <span>全选</span>
        </div>
      <div class="cart-footer-center"><span>合计：</span><span class="total-price">￥{{totalPrice}}</span></div>
      <div class="cart-footer-right"><span class="goto-pay">结算({{totalNum}})</span></div>
    </div>
  </div>
</template>

<script>
// 引入MyHeader组件
import MyHeader from '@/components/MyHeader'

export default {
  // 注入组件
  components: {
    MyHeader
  },
  data () {
    return {
      // 当前购物商品
      currentCart: []
    }
  },
  computed: {
    /* 全选计算属性 */
    isSelectAll () {
      /* 初始化全选状态 */
      let isAll = true

      // 只要商品数组中有一个未选中状态，就给计算属性一个false
      if (this.currentCart.length === 0) {
        isAll = false
      } else {
        this.currentCart.map(item => {
          if (!item.selectState) {
            isAll = false
          }
        })
      }

      return isAll
    },
    /* 总价计算属性 */
    totalPrice () {
      // 初始定义总价
      let total = 0
      // 总价 = 每样被选中商品的数量 * 商品单价 之和
      this.currentCart.map(item => {
        if (item.selectState) {
          total += item.price * item.num
        }
      })
      return total
    },
    /* 总商品数量计算属性 */
    totalNum () {
      // 初始化
      let total = 0
      // 需结算的商品数量为被选中的商品数量
      if (this.currentCart.length !== 0) {
        this.currentCart.map(item => {
          if (item.selectState) {
            total += item.num
          }
        })
      }
      return total
    }
  },
  created () {
    /* 初始化购物车页面 */
    this.initCart()
  },
  methods: {
    /* 初始化购物车页面 */
    initCart () {
      // 先获取本地存储localStorage -- 购物车已添加商品
      let cartsList = JSON.parse(localStorage.getItem('cartsList') || '[]')

      // 当前购物商品
      this.currentCart = cartsList
    },
    /* 切换全选的状态 */
    toggleAll () {
      // 全选状态反转
      let isAll = this.isSelectAll

      // 各商品的选中状态与全选保持一致
      this.currentCart.map(item => {
        item.selectState = !isAll
      })
    },
    /* 切换单商品选中状态 */
    toggleSingle (index) {
      this.currentCart[index].selectState = !this.currentCart[index].selectState
    },
    /* 商品数量增加 */
    handlePlus (index) {
      this.currentCart[index].num++
    },
    /* 商品数量减少 */
    handleMinus (index) {
      this.currentCart[index].num--

      if (this.currentCart[index].num <= 0) {
        this.currentCart.splice(index, 1)
      }
    },
    /* 逛逛页面跳转 -- 调到商品列表 */
    jumpTo () {
      this.$router.push({ name: 'goodsList' })
    }
  },
  /* 离开页面执行的函数 */
  beforeDestroy () {
    /* 将用户操作购物车后的信息更新后本地存储 */
    localStorage.setItem('cartsList', JSON.stringify(this.currentCart))
  }

}
</script>

<style lang="scss" scoped>
.cart {
  height: 320px;
  margin-top: 45px;
  font-size: 14px;
  .empty-cart {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    height: 100%;
    .empty-cart-img {
      height: 90px;
      width: 90px;
    }
    .empty-cart-text {
      font-size: 20px;
      color: #999;
      padding: 15px 0;
    }
    .btn {
      font-size: 20px;
      padding: 15px 55px;
      text-align: center;
      margin: 0 auto;
      border-radius: 10px;
      background: #ed601b;
      color: #fff;
    }
  }
  .cart-container {
    display: flex;
    align-items: center;
    margin: 5px;
    padding: 8px 10px;
    border: 1px solid #eeeeee;
    border-radius: 20px;
    .toogle {
      width: 20px;
      height: 20px;
      line-height: 20px;
      font-size: 20px;
    }
    .icon-checkbox-marked-circ {
      color: #ff5500;
    }
    .icon-checkbox-blank-circle-outline {
      color: #666666;
    }
  }
  .item-detail {
    display: flex;
    flex: 1;
    img {
      margin: 2px 0 2px 5px;
      width: 90px;
      height: 90px;
    }
    .item-info {
      width: 100%;
      padding: 5px 10px;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      h3 {
        font-size: 16px;
        font-weight: 400;
      }
      .pay {
        display: flex;
        justify-content: space-between;
        .pay-price {
          color: #ff5500;
          font-weight: bold;
          line-height: 30px;
        }
        .edit-quantity {
          display: flex;
          height: 30px;
          line-height: 30px;
          .operate-btn {
            padding: 0 10px;
            font-size: 20px;
            color: #bababa;
          }
          .btn-minus {
            font-size: 14px;
          }
        }
      }
    }
  }
  .cart-footer {
    display: flex;
    justify-content: space-between;
    position: fixed;
    bottom: 0;
    left: 0;
    height: 50px;
    line-height: 50px;
    width: 100%;
    border-top: 1px solid #e7e7e7;
    background-color: #fff;
    .cart-footer-left {
      display: flex;
      justify-content: cneter;
      align-items: center;
      span {
        display: block;
        height: 50px;
        padding: 0 5px;
      }
    }
    .cart-footer-center {
      .total-price {
        color: #ff5500;
        font-weight: bold;
      }
    }
    .cart-footer-right {
      display: flex;
      flex-direction: column;
      padding: 0 20px;
      background-color: #ff5500;
      .goto-pay {
        color: #fff;
      }
    }
    .icon-checkbox-marked-circ {
      color: #ff5500;
    }
    .icon-checkbox-blank-circle-outline {
      color: #666666;
    }
  }
}
</style>
